#include "app_uart_bufferb.h"



UART_BUFQ1 dwp_buf1;
UART_BUFQ2 dwp_buf2;

#define HEAD1 0x55
#define HEAD2 0x88
uint8_t enqueue_buf1(uint8_t data)
{
    uint8_t bFull;
    ENQUEUE3(dwp_buf1,UART_BUFFER_QUEUE_LEN,data,bFull);
    
    /*
    {
    bFull = IS_QUEUE_FULL(dwp_buf1,UART_BUFFER_QUEUE_LEN);                                             \
    if(bFull)                                                                                \
    {                                                                                        \
        dwp_buf1.m_u16Length--;                                                              \
        dwp_buf1.m_u16Start = (dwp_buf1.m_u16Start + 1)%UART_BUFFER_QUEUE_LEN;                         \
    }                                                                                        \
                                                                                             \
    dwp_buf1.Object[(dwp_buf1.m_u16Start + dwp_buf1.m_u16Length)%UART_BUFFER_QUEUE_LEN] = data;    \
    dwp_buf1.m_u16Length ++;
    }*/
    return bFull;
}
uint8_t enqueue_buf2(uint8_t data)
{
    uint8_t bFull;
    ENQUEUE3(dwp_buf2,UART_BUFFER_QUEUE2_LEN,data,bFull);
    return bFull;
}


uint8_t dequeue_buf1(uint8_t *datap)
{
    uint8_t isOK;
    uint8_t data = 0;
    DEQUEUE(dwp_buf1,UART_BUFFER_QUEUE_LEN,data,isOK);
    *datap = data;
    return isOK;
}

uint8_t dequeue_buf2(uint8_t *datap)
{
    uint8_t isOK;
    uint8_t data = 0;
    DEQUEUE(dwp_buf2,UART_BUFFER_QUEUE2_LEN,data,isOK);
    *datap = data;
    return isOK;
}

typedef uint8_t (*enqueue_buf_funp)(uint8_t data);
typedef uint8_t (*dequeue_buf_funp)(uint8_t *datap);  

uint8_t  app_uart_data_enqueue(uint8_t *data,uint8_t len,uint8_t pri)
{
    uint8_t isfull = 0;
    
    enqueue_buf_funp enqueuebuf_funp;
    switch(pri)
    {
        case 0:
            enqueuebuf_funp = enqueue_buf1;
            break;
        case 1:
            enqueuebuf_funp = enqueue_buf2;
            break;
    }
    uint32_t cnt = 0; 
    isfull |= (*enqueuebuf_funp)(HEAD1);
    isfull |= (*enqueuebuf_funp)(HEAD2);
    isfull |= (*enqueuebuf_funp)(len);
    isfull |= (*enqueuebuf_funp)(~len);
    for(cnt = 0;cnt<len;cnt++)
    {
        isfull |= (*enqueuebuf_funp)(data[cnt]);
    }
    return isfull;
}

int8_t  app_uart_data_dequeue(uint8_t *data,uint8_t *len,uint8_t pri)
{
    uint8_t isok = 0;
    dequeue_buf_funp dequeuebuf_funp;
    uint8_t datahead[4];
    switch(pri)
    {
        case 0:
            dequeuebuf_funp = dequeue_buf1;
            break;
        case 1:
            dequeuebuf_funp = dequeue_buf2;
            break;
    }
    
    if((*dequeuebuf_funp)(datahead)==0)
    {
        return -1;
    }
    else if(datahead[0] != HEAD1)
    {
        return -2;
    }
    if((*dequeuebuf_funp)(datahead+1)==0)
    {
        return -1;
    }
    else if(datahead[1] != HEAD2)
    {
        return -3;
    }
    if((*dequeuebuf_funp)(datahead+2)==0)
    {
        return -1;
    }
    if((*dequeuebuf_funp)(datahead+3)==0)
    {
        return -1;
    }
    else
    {
        uint8_t data1,data2;
        data1=datahead[3];
        data2=(~datahead[2]);
        if(data1!= data2)
            return -4;
    }
    uint32_t cnt = 0; 
    *len = datahead[2];
    for(cnt = 0;cnt<*len;cnt++)
    {
        if((*dequeuebuf_funp)(data+cnt)==0)
        {
            return -5;
        }
    }
    return 1;
}